/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.ProductTypeBean;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.ProductTypeDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.CodeRule;
import com.minierp.model.ProductType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class ProductTypeFormCtr extends SimpleFormController {
	private ProductTypeDao productTypeDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		refData.put("productTypeList", this.productTypeDao.getList(true));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("PRODUCT_TYPE");
		if (cr == null) {
			throw new Exception("Code rule not found for productType");
		} else {
			ProductTypeBean form = new ProductTypeBean();
			form.setCodeRule(cr);
			if (id != null && !id.equals("")) {
				ProductType parentId1 = (ProductType) this.productTypeDao.get(
						ProductType.class, Long.parseLong(id));
				form.setProductType(parentId1);
			} else {
				form.setProductType(new ProductType());
				String parentId = request.getParameter("parent_id");
				if (parentId != null && !parentId.equals("")) {
					form.getProductType().setParentId(Long.parseLong(parentId));
				}
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String id = request.getParameter("id");
		String alertMsg = "";
		ProductTypeBean form = (ProductTypeBean) command;
		ProductType obj = form.getProductType();
		obj.setNameSpell(PageHelper.getPinYinHeadChar(obj.getTypeName())
				.toUpperCase());
		String oldParentIdStr = "";
		if (obj.getParentIdStr() != null && !obj.getParentIdStr().equals("")) {
			oldParentIdStr = obj.getParentIdStr();
		}

		String parentIdStr = "";
		ProductType mv;
		if (obj.getParentId() != 0L) {
			mv = (ProductType) this.productTypeDao.get(ProductType.class,
					obj.getParentId());
			if (mv != null) {
				if (mv.getParentIdStr() != null
						&& !mv.getParentIdStr().equals("")) {
					parentIdStr = mv.getParentIdStr() + mv.getId() + ";";
				} else {
					parentIdStr = ";" + mv.getId() + ";";
				}
			}
		}

		obj.setParentIdStr(parentIdStr);
		String continueFlag;
		if (id != null && !id.equals("")) {
			this.productTypeDao.update(obj);
			if (!oldParentIdStr.equals(parentIdStr)) {
				List arg15 = this.productTypeDao.getAllChildren(obj.getId());
				continueFlag = "";
				if (oldParentIdStr.equals("")) {
					continueFlag = ";" + obj.getId() + ";";
				} else {
					continueFlag = oldParentIdStr + obj.getId() + ";";
				}

				String newStr = "";
				if (parentIdStr.equals("")) {
					newStr = ";" + obj.getId() + ";";
				} else {
					newStr = parentIdStr + obj.getId() + ";";
				}

				for (int i = 0; i < arg15.size(); ++i) {
					ProductType parentId = (ProductType) arg15.get(i);
					parentId.setParentIdStr(parentId.getParentIdStr()
							.replaceAll(continueFlag, newStr));
					this.productTypeDao.update(parentId);
				}
			}

			alertMsg = "修改成功！";
		} else {
			obj.setId(this.idGenerator.getId(ProductType.class.getName()));
			if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
				obj.setTypeCode(PageHelper.generateCode(this.serialNumberDao,
						form.getCodeRule(), "", 0L));

				for (mv = this.productTypeDao.get(obj.getTypeCode()); mv != null; mv = this.productTypeDao
						.get(obj.getTypeCode())) {
					obj.setTypeCode(PageHelper.generateCode(
							this.serialNumberDao, form.getCodeRule(), "", 0L));
				}
			}

			this.productTypeDao.save(obj);
			alertMsg = "增加成功！";
		}

		String arg16 = request.getParameter("parent_id");
		if (arg16 == null) {
			arg16 = "";
		}

		continueFlag = request.getParameter("continue_flag");
		ModelAndView arg17;
		if (continueFlag != null && continueFlag.equals("1")) {
			arg17 = new ModelAndView(
					"redirect:/base/ProductTypeNew.ihtm?parent_id=" + arg16);
		} else {
			arg17 = new ModelAndView(this.getSuccessView());
			arg17.addObject("returnValue", String.valueOf(obj.getId()));
		}

		EncCommonHelper.setAlertMessage(request, alertMsg);
		return arg17;
	}

	public ProductTypeDao getProductTypeDao() {
		return this.productTypeDao;
	}

	public void setProductTypeDao(ProductTypeDao productTypeDao) {
		this.productTypeDao = productTypeDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}
}